package com.iknight.theory.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iknight.theory.api.TeTranService;
import com.iknight.theory.mapper.*;
import com.iknight.theory.model.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeTranServiceImpl implements TeTranService<TETranModel, String> {
    private final static Logger logger = LogManager.getLogger(TeTranServiceImpl.class);
    @Autowired
    private TeTranMapper<TETranModel, String> teTranMapper;
    @Autowired
    private TeLineMapper<TELineModel, String> teLineMapper;
    @Autowired
    private TeRecordMapper<TPRecordModel, String> teRecordMapper;
    @Autowired
    private TeConsMapper<TEConsModel, String> teConsMapper;
    @Autowired
    private TeLineSegMapper<TELineSegModel, String> teLineSegMapper;
    @Autowired
    private TeLinePoleMapper<TELinePoleModel, String> teLinePoleMapper;
    @Autowired
    private TeContainerMapper<TEConsModel, String> teContainerMapper;
    @Autowired
    private TeLineConsMapper<TELineConsModel, String> teLineConsMapper;
    @Autowired
    private TPCurrentMapper<TPCurrentModel, String> tpCurrentMapper;

    @Override
    public void save(TETranModel record) {
        if (null == teTranMapper.selectOne(record.getTran_id())) {
            teTranMapper.insert(record);
        } else {
            teTranMapper.update(record);
        }
    }

    @Transactional
    @Override
    public void delete(String pkey) {
        teRecordMapper.removeDataByEquipId(pkey);//删除记录
        //查询线路
        Map param = new HashMap();
        param.put("tran_id", pkey);
        List<TELineModel> lineLs = teLineMapper.selectByCond(param);
        if (lineLs.size() > 0) {

            //删除分段
            //删除杆塔
            //删除用户
            for (TELineModel line : lineLs) {

                String lineId = line.getLine_id();

                //删除用户电量
                List<TEConsModel> cons = teConsMapper.getConsByLineId(lineId);
                for (TEConsModel con : cons) {
                    teRecordMapper.removeDataByEquipId(con.getCons_id());
                    teConsMapper.delete(con.getCons_id());
                }

                //删除分段电量
                Map lineParam = new HashMap();
                lineParam.put("line_id", lineId);
                List<TELineSegModel> segs = teLineSegMapper.selectByCond(lineParam);

                for (TELineSegModel seg : segs) {
                    teRecordMapper.removeSegRecordBySegId(seg.getSeg_id());
                }

                teLinePoleMapper.removeDataByLineId(lineId);
                teLineSegMapper.removeDataByLineId(lineId);
                teLineConsMapper.removeDataByLineId(lineId);
                teContainerMapper.removeDataByLine(lineId);

                teLineMapper.delete(lineId);
            }
        }

        teRecordMapper.removeTranRecordByTranId(pkey);
        tpCurrentMapper.removeRecordByTranId(pkey);
        //删除变压器
        teTranMapper.delete(pkey);
    }

    @Override
    public TETranModel selectOne(String pkey) {
        return teTranMapper.selectOne(pkey);
    }

    @Override
    public PageInfo<TETranModel> selectPage(Map<String, Object> params, int pageStart, int pageSize) {
        PageHelper.startPage(pageStart, pageSize, true);
        List<TETranModel> result = teTranMapper.selectByCond(params);
        PageInfo<TETranModel> page = new PageInfo<>(result);
        return page;
    }
}
